package com.example.components.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * User: lu.shuan Date: 2020/7/16 Time: 18:46 Version: 1.0 Modified By: Description:
 */
public final class FileUtils {

    private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);

    private FileUtils() {

    }

    /**
     * 创建文件
     *
     * @param fileName
     * @return
     */
    public static File createFile(String fileName) {
        File file = new File(fileName);
        String path = fileName.substring(0, fileName.lastIndexOf("/"));
        try {
            if (!new File(path).exists()) {
                new File(path).mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            }
        } catch (IOException e) {
            logger.error(e.getMessage());
            return file;
        }
        return file;
    }

    /**
     * 清空已有的文件内容，以便下次重新写入新的内容
     *
     * @param fileName
     */
    public static void clearInfoForFile(String fileName) {
        File file = new File(fileName);
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter fileWriter = new FileWriter(file);
            fileWriter.write("");
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
    }

    /**
     * 读取文件内容，文件中的内容为String
     */
    public static List<String> readInfoFromFile(String fileName) {
        File file = new File(fileName);
        if (!file.exists()) {
            return null;
        }
        List<String> resultStr = new ArrayList<String>();
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            String str = null;
            while (null != (str = bufferedReader.readLine())) {
                resultStr.add(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resultStr;
    }

    public static void translateFileInfo(String fileName1, String fileName2) {

        //写入中文字符时解决中文乱码问题
        FileReader fr = null;
        BufferedReader br = null;
        FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;

        try {
            fr = new FileReader(fileName1);
            br = new BufferedReader(fr);
            String line = "";
            String[] arrs = null;
            while ((line = br.readLine()) != null) {
                arrs = line.split(",");
                System.out.println(Arrays.asList(arrs).toString());
            }
            fos = new FileOutputStream(new File(fileName2));
            osw = new OutputStreamWriter(fos, "UTF-8");
            bw = new BufferedWriter(osw);
            for (String arr : arrs) {
                bw.write(arr + "\t\n");
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //注意关闭的先后顺序，先打开的后关闭，后打开的先关闭
//            bw.close();
//            osw.close();
//            fos.close();
//            br.close();
//            fr.close();
        }
    }

    public static void translateFileInfo2(String fileName1, String fileName2) {
        //写入中文字符时解决中文乱码问题
        try (FileReader fr = new FileReader(fileName1); BufferedReader br = new BufferedReader(fr); FileOutputStream fos = new FileOutputStream(new File(fileName2)); OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); BufferedWriter bw = new BufferedWriter(osw)) {
            String line = "";
            while ((line = br.readLine()) != null) {
                if (!line.contains("!!")) {
                    bw.write(line.replace("forName", "for") + System.lineSeparator());
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
